function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { dataUrlToBlob, filePicker, fileToDataUrl } from '../file';
export var downloadImage = function downloadImage(url) {
  var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
    useCache: false
  };
  var callback = arguments.length > 2 ? arguments[2] : undefined;
  var useCache = args.useCache;
  var image = new Image();
  image.setAttribute('crossOrigin', 'anonymous');
  var src;

  if (useCache || url.indexOf('data:') === 0) {
    src = url;
  } else {
    var time = new Date().getTime();
    src = "".concat(url).concat(url.indexOf('?') === -1 ? '?' : '&').concat(time, "=").concat(time);
  }

  image.src = src;
  var callbackFlag = false;
  var success = false;

  var _callback = function _callback() {
    if (!callbackFlag && callback) {
      callback(image, success);
    }

    callbackFlag = true;
  };

  image.onload = function () {
    success = true;

    _callback();
  };

  image.onerror = _callback;
  image.onabort = _callback;
};
export var imageToDataUrl = function imageToDataUrl(image, args) {
  var maxWidth = args.maxWidth,
      maxHeight = args.maxHeight,
      _args$type = args.type,
      type = _args$type === void 0 ? 'image/png' : _args$type,
      _args$quality = args.quality,
      quality = _args$quality === void 0 ? 1 : _args$quality;
  var originWidth = image.width;
  var originHeight = image.height;
  var targetWidth = originWidth;
  var targetHeight = originHeight;

  if (originWidth > maxWidth || originHeight > maxHeight) {
    if (originWidth / originHeight > maxWidth / maxHeight) {
      targetWidth = maxWidth;
      targetHeight = Math.round(maxWidth * (originHeight / originWidth));
    } else {
      targetHeight = maxHeight;
      targetWidth = Math.round(maxHeight * (originWidth / originHeight));
    }
  }

  var canvas = document.createElement('canvas');
  canvas.width = targetWidth;
  canvas.height = targetHeight;
  var context = canvas.getContext('2d');
  context.clearRect(0, 0, targetWidth, targetHeight);
  context.drawImage(image, 0, 0, targetWidth, targetHeight);
  return canvas.toDataURL(type, quality);
};
export var urlToDataUrl = function urlToDataUrl(url, args, callback) {
  downloadImage(url, args, function (image) {
    if (callback) {
      callback(imageToDataUrl(image, args));
    }
  });
};
/**
 * 图片选择
 *
 * @param {function} complete 回调事件
 */

export var imagePicker = function imagePicker(callback, option) {
  var _a = option || {},
      compress = _a.compress,
      otherOption = __rest(_a, ["compress"]);

  filePicker(compress ? function (fileObjs) {
    var results = [];

    var _loop = function _loop(i, length) {
      var fileObj = fileObjs[i];
      fileToDataUrl(fileObj.file, function (dataUrl) {
        downloadImage(dataUrl, {
          useCache: true
        }, function (image) {
          var compressDataUrl = imageToDataUrl(image, compress);
          var compressBlob = dataUrlToBlob(compressDataUrl);
          var filename = fileObj.file.name || '';
          var index = filename.lastIndexOf('.');

          if (index !== -1) {
            filename = filename.substring(0, index);
          }

          filename = "".concat(filename).concat(compress.type ? compress.type.replace('image/', '.') : '.png'); // @ts-ignore

          compressBlob.name = filename;
          results[i] = {
            file: compressBlob,
            originFile: fileObj.file,
            filename: filename,
            dataUrl: compressDataUrl,
            originDataUrl: dataUrl
          };

          if (results.filter(function (item) {
            return !!item;
          }).length === length) {
            callback(results);
          }
        });
      });
    };

    for (var i = 0, length = fileObjs.length; i < length; i += 1) {
      _loop(i, length);
    }
  } : callback, _extends(_extends({}, otherOption), {
    accept: 'image/*'
  }));
};